home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / menu-new.tar / menu-new / menu / files.c < prev    next >
C/C++ Source or Header  |  1993-04-08  |  4KB  |  158 lines

  1. # include <stdio.h>
  2. # include <sys/file.h>
  3. # include <unistd.h>
  4. # include "menu.h"
  5.  
  6. /*    PSC MENU COPYRIGHT NOTICE
  7.  
  8.     Part of PSCMenu
  9.  
  10.     This software is to be considered to be public domain, it
  11. may be copied, modified and parts of it may be used in other programs
  12. as long as this copyright notice remains intact.
  13.  
  14.     Copyright()   PSC - Plymouth State College
  15.     Written by:   Ted Wisniewski 12-9-1990
  16.  
  17. */
  18.  
  19. /*
  20.  *    int check_file(instring)
  21.  *
  22.  *    Parameters:    instring - The Name of the file to be accessed.
  23.  *
  24.  *    Returns:    -1  = Failure, file may not exist.
  25.  *            1   = File is Readable.
  26.  *
  27.  *     Purpose:    Determine if a file is readable.
  28.  *
  29.  *    Last Modify:    3-31-91 (TW)
  30.  */
  31. int check_file(instring)
  32. char *instring;
  33. {
  34.     if(strlen(instring) > 0){
  35.       if((access(instring,R_OK)) == -1)
  36.         return(-1);
  37.       else
  38.         return(1);
  39.     }else
  40.       return(-1);
  41. }
  42.  
  43. /*
  44.  *    read_menu(menu,file_name,num)
  45.  *
  46.  *    Parameters:    menu         - Pointer to the menu to be loaded.
  47.  *            file_name    - Name of the menu file.
  48.  *            num         - The number of menu entries read.
  49.  *
  50.  *    Returns:    None.
  51.  *
  52.  *    Purpose:    Read a menu from file information.
  53.  *    
  54.  *    Last Modify:    3-31-91 (TW)
  55.  */
  56.  
  57. void read_menu(menu,file_name,num)
  58. menu_ent *menu;
  59. char *file_name;
  60. int *num;
  61. {
  62.     FILE *fp;
  63.     int index = 0;
  64.     char buff[BUF_SIZ];
  65.     
  66.     if(menu->key == '@')
  67.        m_flag = TRUE;
  68.     else
  69.        m_flag = FALSE;
  70.     Sprintf(buff,"%s%s",menu_dir,file_name);
  71.     if((access(buff,R_OK|F_OK)) == -1){
  72.        move_csr(26,11);
  73.        hi_lite("Menu file cannot be accessed.");
  74.        continue_it(20);
  75.        erase_line(26,11,30);
  76.     }else{
  77.        fp = fopen(buff,"r");
  78.        menu[index].execdir[0] = 0;
  79.        menu[index].AllowWander = 1;
  80.        while(!feof(fp)){
  81.           Fgets(buff,256,fp);
  82.           rm_lf(buff);
  83.           if(!feof(fp) && (index <= 7)){
  84.         switch (buff[0]){
  85.           case '$':    /* Menu Header */
  86.             menu[index].key = buff[0];
  87.             Strcpy(menu[index].desc,buff+2);
  88.             index++;
  89.             menu[index].execdir[0] = 0;
  90.             menu[index].AllowWander = 1;
  91.           break;
  92.           case '?':     /* Menu Comment (User sees)     */
  93.             Strcpy(menu[index].desc,buff+2);
  94.             chop_str(menu[index].desc,36);
  95.             index++;
  96.             menu[index].execdir[0] = 0;
  97.             menu[index].AllowWander = 1;
  98.           break;
  99.           case '#':    /* Help file name         */
  100.             Strcpy(menu[index].help_fil,buff+2);
  101.           break;
  102.           case '+':    /* Exec in pipe            */
  103.             menu[index].key = buff[0];
  104.             Strcpy(menu[index].cmd,buff+2);
  105.           break;
  106.           case '!':        /* Strings used to build a bourne*/
  107.                     /* shell script to be run.     */
  108.                     /* Unimplemented as of yet.     */
  109.               break;
  110.           case '%':        /* '%' Do not use continue line  */
  111.           case '*':        /* This command to be executed   */
  112.                     /* in a sub-shell. Use Continue  */
  113.             menu[index].key = buff[0];
  114.             Strcpy(menu[index].cmd,buff+2);
  115.           break;
  116.           case '@':        /* This Menu is a main Menu      */
  117.           case '&':        /* This Menu is a regular Menu.  */
  118.             menu[index].key = buff[0];
  119.             Strcpy(menu[index].cmd,buff+2);
  120.           break;
  121.           case '1':        /* Ask for a file name        */
  122.             menu[index].key = buff[0];
  123.             Strcpy(menu[index].cmd,buff+2);
  124.           break;
  125.           case '2':        /* Ask for two file Names    */
  126.             menu[index].key = buff[0];
  127.             Strcpy(menu[index].cmd,buff+2);
  128.           break;
  129.           case '3':        /* Ask Whom            */
  130.             menu[index].key = buff[0];
  131.             Strcpy(menu[index].cmd,buff+2);
  132.           break;
  133.           case '4':        /* Ask what?            */
  134.             menu[index].key = buff[0];
  135.             Strcpy(menu[index].cmd,buff+2);
  136.           break;
  137.           case '5':        /* Use visual directory in shell */
  138.             menu[index].key = buff[0];
  139.             Strcpy(menu[index].cmd,buff+2);
  140.           break;
  141.           case '6':        /* Use visual in pipe        */
  142.             menu[index].key = buff[0];
  143.             Strcpy(menu[index].cmd,buff+2);
  144.           break;
  145.           case 'd':        /* Set working directory for command */
  146.             Strcpy(menu[index].execdir,buff+2);
  147.             if (strlen(menu[index].execdir)!=0)
  148.                 menu[index].AllowWander = ('/' != menu[index].execdir[
  149.                   strlen(menu[index].execdir)-1]);
  150.           break;
  151.         }
  152.           }
  153.         }
  154.     (void) fclose(fp);
  155.     *num = index;
  156.     }
  157. }
  158.